/*
   Copyright (c) [2023] [魔凤啸天]
   [MFUI] is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details.  
*/
#pragma once
#include "button.hpp"

namespace MFUI{
    class ListLayout:public Button{
		protected:
		static void scrollcallback(Window*,ListLayout*,SDL_Event*);
		void _losefocus(Window* w)override;
		~ListLayout()override;

		SDL_FRect itemrect;
		unsigned int displayitem;//虚拟列表显示的节点数 始终小于等于最大数
		size_t begin;
		SDL_FRect scrollrect;//displayitem/total*w|h
		float unit,total;
        SDL_Color background;
		bool dragged,scrollchanged,inscroll;
		
		//返回是否执行了滚动 仅判断newindex是否等于begin以及是否超出范围
		bool _scroll(Window* w,size_t newindex);

		public:
		ListLayout(unsigned int ndisplay,SDL_Color back,std::initializer_list<Node*>& children);
		void setdisplayitem(Window* w,unsigned int ndisplay);
		unsigned int getdisplayitem()const;

		void onremove(Window* w)override;
		void onadd(Window* w)override;
		void mouseexited(Window* w)override;
		bool confirmdraw()override;
		void removechild(Window* w,size_t index)override;
		void removechildren(Window* w,size_t start,size_t end)override;
		void popchildren(Window* w,size_t n)override;
		void addchild(Window* w,Node* n)override;
		void addchildren(Window* w,std::initializer_list<Node*> l)override;
		void insertchild(Window* w,Node* n,size_t index)override;
		void insertchildren(Window* w,std::initializer_list<Node*> l,size_t index)override;
		void swapchild(size_t a,size_t b)override;
		
		virtual void scroll(Window* w,int x,int y)=0;
	};
	class VListLayout:public ListLayout{
		public:
		VListLayout(unsigned int ndisplay,SDL_Color back,std::initializer_list<Node*> children);

		void draw(Renderer* r,bool updatecache)override;
		void scroll(Window* w,int x,int y)override;
		void scrollto(Window* w,float p);
		void scrollto(Window* w,size_t index);
		void pressed(Window* w,SDL_FPoint& p)override;
		void released(Window* w,SDL_FPoint& p)override;
		void mousemove(Window* w,SDL_FPoint p)override;
	};
	class HListLayout:public ListLayout{
		public:
		HListLayout(unsigned int ndisplay,SDL_Color back,std::initializer_list<Node*> children);

		void draw(Renderer* r,bool updatecache)override;
		void scroll(Window* w,int x,int y)override;
		void scrollto(Window* w,float p);
		void scrollto(Window* w,size_t index);
		void pressed(Window* w,SDL_FPoint& p)override;
		void released(Window* w,SDL_FPoint& p)override;
		void mousemove(Window* w,SDL_FPoint p)override;
	};
}